<?php

Class Youtube {
    
    var $video_data;
    
    function __construct($array = null) {
        if (!empty($array)) {
            foreach ($array as $key => $value) {
                $this->$key = $value;
            }
        }
    }
    
    function set_youtube_id($video_ID){
        $JSON = file_get_contents("http://gdata.youtube.com/feeds/api/videos?q={$video_ID}&alt=json");
        $this->video_data = json_decode($JSON);
        return $this->video_data;
    }
    
    function get_youtube_detail($video_ID){
        return json_encode(file_get_contents("http://gdata.youtube.com/feeds/api/videos?q={$video_ID}&alt=json"));
    }
    
    function get_video_time($data = null){
        if(empty($data)){
            $data = $this->video_data;
        }
        return $data->{'feed'}->{'entry'}[0]->{'media$group'}->{'yt$duration'}->{'seconds'};
    }
    
    function get_video_time_format($data = null){
        if(empty($data)){
            $data = $this->video_data;
        }
        $video_time = $this->get_video_time($data);
        $buffer = round($video_time / 60);
        $min = ($buffer > 0) ? $buffer.":": '';
        $sec = sprintf("%02d",$video_time % 60);
        $video_time = $min.$sec;
        return $video_time;
    }
    
    function get_rate($data = null){
        if(empty($data)){
            $data = $this->video_data;
        }
        return $data->{'feed'}->{'entry'}[0]->{'gd$rating'}->{'average'};
    }
    
    function get_view($data = null){
        if(empty($data)){
            $data = $this->video_data;
        }
        return $data->{'feed'}->{'entry'}[0]->{'yt$statistics'}->{'viewCount'};
    }
    function echo_player(){
        echo <<<HTML
        <iframe width="420" height="315" src="http://www.youtube.com/embed/iu-L6zWwe-Q" frameborder="0" allowfullscreen></iframe>
HTML;
    }
}